package com.mdl.common.util.rsa;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.security.spec.InvalidKeySpecException;

/**
 * Project Name: test
 * File Name: SignUtil
 * Package Name: rsa
 * Date: 2018/9/10  17:02
 * Copyright (c) 2018, tianyul All Rights Reserved.
 * 加密--RSA
 */
public class SignUtil {

    private static final String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDWyvJdPFVD/o87fsKjA9mMtoolLVUx/HZj7RS/A5cFXNCnGdoxvcWx0HLv3FpFyQjkrwGVAXa91+7vRHBSb0v4vaLd7WAGzpgYAKQHPvyXB7/GmAPVtTOK88W5UR2dTLOrGnRyNXfE1t0VY/ku79UVyZcsdJOYOINnPvKAtx7OLwIDAQAB";
    private static final String privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBANbK8l08VUP+jzt+wqMD2Yy2iiUtVTH8dmPtFL8DlwVc0KcZ2jG9xbHQcu/cWkXJCOSvAZUBdr3X7u9EcFJvS/i9ot3tYAbOmBgApAc+/JcHv8aYA9W1M4rzxblRHZ1Ms6sadHI1d8TW3RVj+S7v1RXJlyx0k5g4g2c+8oC3Hs4vAgMBAAECgYAjl8XJxXGrrsc1aLAAK2cldio2NujjO56yuFqzvHiOHu9EgXgyvevAp3YkYTNkVQfwPT/Z3UA7/UhOxdyHDIJeC7Bk7zmpcZsgMBaZHc2fa7ALla9LaYSKoKSym4HA1kwVyifMHy/Wf/SJDWVi0kecH3ORqFgypyLHxFFdI6EXQQJBAP8pT4I1PlN5bYjjttX1ULdFre1Uf/rO68g1T2QnSISq5Hy/7EsyGGyjMZe19H2HTN7dsXaXkG8FCSRMhM2rlxcCQQDXf6uoZSIrPe2fGWQDaZdfq53Dyu+vpR0FTEJRj+xOSGuSipqu3qsBZbYdlAfR864pDuHFdeDELfaYB1ot8nCpAkEArqhcYEiJzSHFNHqK4JH3Gjx7/YRlxgx1oKm9GlU5cnZ3yFynJiG4TkS24Z4PKQ3ANJ2TqBW/K/9KbqpiDVZzKQJBAKbJjm8nzxlOtfHUcf7eI6WNKu7Vy0rTbTGjgxSjYs536KIjmnLLrejH8vrTjnXltx7aAM2WXV8Y/+lMj3ROVvkCQG5O3fDtrRsyuwW3h5anOJzrsmzrnfNABeaEmolXEZugld9z3clv/CyJYZY/9zILt1Fz319geaBgmGD/hUhV5dA=";

//    public static void main(String[] args) throws InvalidKeySpecException, SignatureException, NoSuchAlgorithmException, InvalidKeyException, IOException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException {
//        //签名 验签
////        checkSign();
//        //公钥加密私钥解密
////        pubEncrypt();
//        //私钥加密 公钥解密
//        priEncrypt();
//    }

    private static void priEncrypt() throws NoSuchPaddingException, NoSuchAlgorithmException, IOException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException, InvalidKeySpecException {
        RSAUtil rsaUtil = new RSAUtil();
        String info = "哈哈哈哈";

        //模拟发送方 私钥加密
        byte[] data = rsaUtil.encryptByPrivateKey(info.getBytes(), privateKey);
        String send = rsaUtil.encryptBase64(data);
        System.out.println(send);

        //公钥机密
        String recieve = send;
        byte[] result = rsaUtil.decryptByPublicKey(rsaUtil.decryptBase64(recieve), publicKey);
        System.out.println(new String(result, "UTF-8"));
    }

    /**
     * 公钥加密 私钥解密
     */
    public static void pubEncrypt() throws NoSuchPaddingException, NoSuchAlgorithmException, IOException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException, InvalidKeySpecException {
        RSAUtil rsaUtil = new RSAUtil();
        String world = "world";

        //模拟消息发送方 公钥加密访问
        byte[] data = rsaUtil.encryptByPublicKey(world.getBytes(), publicKey);
        String send = rsaUtil.encryptBase64(data);
        System.out.println(send);

        //接受到数据
        String recieve = send;
        byte[] result = rsaUtil.decryptByPrivateKey(rsaUtil.decryptBase64(recieve), privateKey);

        System.out.println(new String(result));
    }

    /**
     * 验签
     * @throws InvalidKeySpecException
     * @throws SignatureException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws IOException
     */
    public static void checkSign() throws InvalidKeySpecException, SignatureException, NoSuchAlgorithmException, InvalidKeyException, IOException {
        RSAUtil rsaUtil = new RSAUtil();
        String data = "hello";
        String sign = rsaUtil.sign(data.getBytes() ,privateKey);
        System.out.println(sign);

        boolean flag = rsaUtil.verify(data.getBytes(), publicKey, sign);
        System.out.println(flag);
    }
}
